package stat;

public class HPDerivedStat extends DerivedStat {

    private int cachedMax;
    
    public HPDerivedStat( StatType st, Stat... toCache) {
        super(st, toCache);
        if(toCache.length > 0) {
            setValue(getFromCache(StatType.MAXHP));
            cachedMax = getFromCache(StatType.MAXHP);

        }
        //compute();
    }
    
    public HPDerivedStat( StatType st, int value, Stat... toCache) {
        super(st, value, toCache);
        if(toCache.length > 0) {
            setValue(getFromCache(StatType.MAXHP));
            cachedMax = getFromCache(StatType.MAXHP);
        }
        //compute();
    }
    
    /**
     * compute the new hp value
     */
    public void compute() {
        if(getFromCache(StatType.MAXHP) > cachedMax) {
            setValue(getValue() + Math.abs(getFromCache(StatType.MAXHP) - cachedMax));
            cachedMax = getFromCache(StatType.MAXHP);
        }
    }
    
    /**
     * set the value of the hp, making sure it's valid first
     */
    public void setValue(int val) {
        if(shouldDerive() && getFromCache(StatType.MAXHP) < val ) {
            val = getFromCache(StatType.MAXHP);
        }
        
        super.setValue(val);
        if(shouldDerive() && getValue() <= 0 ) {
            super.setValue(getFromCache(StatType.MAXHP));
        }
    }
    
}
